package com.huizhan.web.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import io.swagger.annotations.*;
import java.io.*;
import javax.servlet.http.HttpServletRequest;

import com.huizhan.web.model.*;
import com.huizhan.web.view.Util;
import com.huizhan.web.view.*;

/**
 * 上传API
 */
@Api(tags = "Upload", description = "上传文件")
@RestController
public class UploadController {

    @Value("${uploadFile.location}")
    private String uploadFileLocation;//上传文件保存的本地目录，使用@Value获取全局配置文件中配置的属性值
    
    @Value("${uploadFile.resourceHandler}")
    private String resourceHandler;//请求 url 中的资源映射，不推荐写死在代码中，最好提供可配置，如 /uploadFiles/**

    /**
     * 上传文件
     * 
     * @param file
     * @return
     */
    @ApiOperation(value = "上传文件")
    @RequestMapping(value = "/api/Upload/File", method = { RequestMethod.POST })
    public ApiResult<String> upload(@RequestParam(value = "file", required = true) MultipartFile files, HttpServletRequest request) {

        ApiResult<String> r = new ApiResult<String>();

        try {
            String fileName = Util.getFileSuffix(files.getOriginalFilename());
            String newFilePath = Util.md5HashCode32(files.getInputStream()) + fileName;
            String c=uploadFileLocation;
            File file = new File(c);
            if(!file.exists()){ 
                file.mkdir();
            }
            c+=newFilePath;
            file = new File(c);
            if(!file.exists()){ 
                FileOutputStream fop = new FileOutputStream(file);
                file.createNewFile();
                byte[] contentInBytes = files.getBytes();
                fop.write(contentInBytes);// 写入本地
                fop.flush();
                fop.close();
            }
            if(request.getServerPort()!=80){
                r.setData(request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath()+resourceHandler.replace("*","")+newFilePath); 
            }else{
                r.setData(request.getScheme() + "://" + request.getServerName()+resourceHandler.replace("*","")+newFilePath); 
            }
        } catch (Exception e) {
            r.setHasError(true);
            r.setMessage(e.getMessage());
        }

        return r;
    }

    
    /**
     * 上传文件base64
     * 
     * @param base64
     * @return
     */
    @ApiOperation(value = "上传文件base64")
    @RequestMapping(value = "/api/Upload/Base64", method = { RequestMethod.POST })
    public ApiResult<String> upload(@RequestBody Base64String base64, HttpServletRequest request) {

        ApiResult<String> r = new ApiResult<String>();

        try {            
             String newFilePath=base64.getFileName();
             File file=new File(uploadFileLocation+newFilePath);
             if(!file.exists()){
                Util.base64ToFile(base64.getBase64(), uploadFileLocation+newFilePath);        
             }
             if(request.getServerPort()!=80){
                 r.setData(request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath()+resourceHandler.replace("*","")+newFilePath); 
             }else{
                r.setData(request.getScheme() + "://" + request.getServerName() +resourceHandler.replace("*","")+newFilePath); 
             }
        } catch (Exception e) {
            r.setHasError(true);
            r.setMessage(e.getMessage());
        }

        return r;
    }
}